package com.aiju.cash.ui.member;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.aiju.app.SimpleHttpCallback;
import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.core.api.MemberPointApi;
import com.aiju.cash.ui.member.adater.MemberPointAdapter;
import com.aiju.cash.ui.member.entity.MemberPoint;
import com.aiju.cash.ui.member.entity.Point;
import com.aiju.cash.util.GsonHelper;
import com.aiju.support.recyclerView.DividerItemDecoration;
import com.aiju.utils.Util;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.loadmore.SwipeRefreshHelper;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 佐为
 * @date: 2017-02-20 15:59
 * @version: V1.0
 * @Description: 会员积分
 */
public class MemberPointsListActivity extends BaseTitleActivity implements
        SwipeRefreshHelper.OnSwipeRefreshListener, OnLoadMoreListener {

    private View useView, getView;

    private RecyclerView recyclerView;
    private SwipeRefreshLayout mSryt;
    private SwipeRefreshHelper mSwipeRefreshHelper;
    private RecyclerAdapterWithHF mAdapter;
    private boolean isLoad;
    private boolean isRefresh;

    private int pageSize = 10;
    private int currentNum = 1;
    private View view;
    private MemberPointAdapter adatper;
    private List<Point> items = new ArrayList<>();

    private int type = 0;

    private String memberPhone;
    private MemberPointApi memberPointApi;
    private MemberPoint memberPoint;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member_list);
        setCenterTitle("会员积分详情");

        memberPhone = getIntent().getStringExtra("memberPhone");
        String points = getIntent().getStringExtra("points");
        getView = findViewById(R.id.ly_point_get);
        useView = findViewById(R.id.ly_point_use);
        TextView textView = (TextView) findViewById(R.id.tv_title_point);
        textView.setText(points);

        getView.setSelected(true);
        getView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setSelected(true);
                useView.setSelected(false);
                type = 0;
                items.clear();
                requsetList();
            }
        });
        useView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setSelected(true);
                getView.setSelected(false);
                type = 1;
                items.clear();
                requsetList();
            }
        });

        view = findViewById(R.id.iv_empty);
        mSryt = (SwipeRefreshLayout) this.findViewById(R.id.sryt_swipe_listview);
        mSryt.setColorSchemeColors(getResources().getColor(R.color.app_title_color));

        recyclerView = (RecyclerView) findViewById(R.id.lv_account_calender);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        adatper = new MemberPointAdapter(items);
        mAdapter = new RecyclerAdapterWithHF(adatper);
        recyclerView.setAdapter(mAdapter);

        mSwipeRefreshHelper = new SwipeRefreshHelper(mSryt);
        mSwipeRefreshHelper.setOnSwipeRefreshListener(this);
        mSwipeRefreshHelper.setOnLoadMoreListener(this);

        memberPointApi = new MemberPointApi(mDialogHelper);
        requsetList();
    }

    @Override
    public void loadMore() {
        isLoad = true;
        currentNum += 1;
        requsetList();
    }

    @Override
    public void onfresh() {
        isRefresh = true;
        currentNum = 1;
        requsetList();
    }

    private void requsetList() {
        if (type == 0) {
            memberPointApi.getPointslist(memberPhone, String.valueOf(currentNum), String.valueOf(pageSize), new SimpleHttpCallback() {
                @Override
                protected void onDataSuccess(String data) {
                    bulidListView(data);
                }
            });
        } else {
            memberPointApi.usedPointslist(memberPhone, String.valueOf(currentNum), String.valueOf(pageSize), new SimpleHttpCallback() {
                @Override
                protected void onDataSuccess(String data) {
                    bulidListView(data);
                }
            });
        }
    }

    private void bulidListView(String data) {
        memberPoint = GsonHelper.convertEntity(data, MemberPoint.class);
        //textView.setText(memberPoint.getSum());
        if (!Util.isEmpty(memberPoint.getList())) {
            if (isRefresh) {
                items.clear();
            }
            items.addAll(memberPoint.getList());
        }
        adatper.setType(type);
        adatper.notifyDataSetChanged();
        completeView(memberPoint.getList());
    }

    /**
     * 加载完成视图动作
     *
     * @param blotterLists
     */
    private void completeView(List<Point> blotterLists) {
        //加载
        if (isLoad) {
            isLoad = false;
            mSwipeRefreshHelper.loadMoreComplete(true);
        }
        //刷新
        if (isRefresh) {
            isRefresh = false;
            mSwipeRefreshHelper.refreshComplete();
            mSwipeRefreshHelper.setLoadMoreEnable(true);
        }

        if (blotterLists.size() < pageSize) {
            mSwipeRefreshHelper.setLoadMoreEnable(false);
        } else {
            mSwipeRefreshHelper.setLoadMoreEnable(true);
        }
        checkIfEmpty();
    }

    private void checkIfEmpty() {
        if (adatper != null && mSwipeRefreshHelper != null) {
            final boolean emptyViewVisible = adatper.isEmpty();
            if (emptyViewVisible) {
                view.setVisibility(View.VISIBLE);
            } else {
                view.setVisibility(View.GONE);
            }
        }
    }
}
